<?php
declare (strict_types = 1);

namespace app\service\logic\common\user;

use app\model\communal\UserCourseCollectModel;
use think\Model;
use app\model\communal\UserExpertFollowModel;
use app\service\logic\BaseService;

final class CourseCollectService extends baseservice
{
    protected string $modelClassName = 'UserCourseCollectModel';

    /**
     * 收藏/取消
     *
     * @param $params
     * @return bool
     */
    public function create($params)
    {
        $model = UserCourseCollectModel::withSearch(['user_id', 'expert_id'], $params)->findorempty();
        if ($model->isEmpty()) {
            // 收藏
            $result = $model->save($params);
        } else {
            // 取消收藏
            $result = $model->delete();
        }

        return $result;
    }

    /**
     * 用户是否收藏该课程
     *
     * @param $userId
     * @param $courseId
     * @return int
     */
    public function isCollect($userId, $courseId)
    {
        $model = UserCourseCollectModel::where(['user_id' => $userId, 'course_id' => $courseId])->findOrEmpty();
        return $model->isEmpty() ? 2 : 1;
    }

    /**
     * 用户已关注课程id
     *
     * @param $userId
     * @return array
     */
    public function getCourseIdsByUserId($userId)
    {
        return UserCourseCollectModel::where('user_id', $userId)->column('course_id');
    }
}
